package com.ia.aistream.config;


import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;
import java.util.List;


/**
 * Author: xiaoyu
 * Date: 2021/2/4 15:36
 * 参考博客：https://www.wangt.cc/2020/11/springboot%E5%90%84%E7%A7%8D%E8%B7%A8%E5%9F%9F%E9%85%8D%E7%BD%AE%E6%94%AF%E6%8C%81/
 */
@Configuration
public class CorsConfig {

    private static String[] originsVal = new String[]{
            "127.0.0.1:8080",
            "localhost:8080",
            "google.com"
    };

    @Bean
    public FilterRegistrationBean corsFilter() {
        System.out.println(123456);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 可以设置部分地址可以进行访问
        List<String> origins = Arrays.asList(originsVal);
        config.setAllowedOrigins(origins);
        // 设置所有地址的请求都可以
        config.addAllowedOrigin("*");
        // 可以设置允许部分请求头信息
//        List<String> headers = Arrays.asList("Authorization",  "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials", "Content-Type", "Access-Control-Request-Method", "Access-Control-Request-Headers");
//        config.setAllowedHeaders(headers);
        // 设置为允许所有请求头信息
        config.addAllowedHeader("*");
        // 可以设置只支持部分请求方式
//        List<String> methods =  Arrays.asList("GET","POST","HEAD","OPTIONS","PUT");
//        config.setAllowedMethods(methods);
        // 设置为支持所有请求方式
        config.addAllowedMethod("*");

        // 可以设置部分请求路径才可以进行访问
//        source.registerCorsConfiguration("/cors/**",config);
        // 设置所有的请求路径都可以访问
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        //设置优先级为最高
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }
//    private CorsConfiguration buildConfig() {
//        System.out.println("==============CorsConfig==1111111111======");
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        corsConfiguration.addAllowedOrigin("*");
//        corsConfiguration.addAllowedHeader("*");
//        corsConfiguration.addAllowedMethod("*");
//        corsConfiguration.setMaxAge(3600L);         // 预检请求的有效期，单位为秒。
//        corsConfiguration.setAllowCredentials(true);// 是否支持安全证书(必需参数)
//        return corsConfiguration;
//    }
//
//    @Bean
//    public CorsFilter corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", buildConfig());
//        return new CorsFilter(source);
//    }
}
